package com.privatetime.common;


public class AbstractPagination<E> {

	private int currentIndex = 1;// current index

	private int totalNum = 0;// the number of all data

	private int totalPages = 0;// total pages

	private int pageSize = 10;// every page size

	private int startRow = 1; // start row from db

	public AbstractPagination() {
		calculateTotalPages();
	}

	public AbstractPagination(int currentIndex, int pageSize) {
		this.currentIndex = currentIndex;
		this.pageSize = pageSize;
		this.startRow = ((currentIndex - 1) * pageSize) + 1;

		calculateTotalPages();
	}

	protected void first() {
		currentIndex = 1;
		startRow = 1;
	}

	protected void last() {
		currentIndex = totalPages;
		startRow = ((currentIndex - 1) * pageSize) + 1;
	}

	protected void next() {
		if (totalNum == 0) {
			return;
		}

		if (currentIndex < totalPages) {
			currentIndex++;
		}
		startRow = ((currentIndex - 1) * pageSize) + 1;
	}

	protected void previous() {
		if (currentIndex == 1) {
			return;
		}

		currentIndex--;
		startRow = ((currentIndex - 1) * pageSize) + 1;
	}

	private void calculateTotalPages() {
		if (totalNum <= pageSize) {
			totalPages = 1;
		} else {
			totalPages = (totalNum % pageSize) == 0 ? (totalNum / pageSize)
					: (totalNum / pageSize) + 1;
		}
	}

	@Override
	public String toString() {
		return "AbstractPagination [currentIndex=" + currentIndex
				+ ", pageSize=" + pageSize + ", startRow=" + startRow
				+ ", totalNum=" + totalNum + ", totalPages=" + totalPages + "]";
	}

}
